package com.lzy.concertticket.interceptor;

import com.lzy.concertticket.threadlocal.RequestThreadLocal;
import com.lzy.concertticket.threadlocal.ResponseThreadLocal;
import com.lzy.concertticket.threadlocal.UserEntityThreadLocal;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Slf4j
@Component
public class RequestResponseInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //  将请求响应对象保存到线程变量中
        RequestThreadLocal.set(request);
        ResponseThreadLocal.set(response);
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        //  将请求响应对象从线程变量中移除
        RequestThreadLocal.remove();
        ResponseThreadLocal.remove();
    }

}
